#include "mainwindow.h"
#include "ui_mainwindow.h"

int MainWindow::flag = 3;
QString MainWindow::path1;
QString MainWindow::path2;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(1300, 600);
    this->setWindowIcon(QIcon(":/Icon/ChfSync"));
    this->setWindowTitle("ChfSync");
    ui->treeWidget->setColumnWidth(0,50);
    ui->treeWidget->setColumnWidth(1,300);
    ui->treeWidget->setColumnWidth(2,50);
    ui->treeWidget->setColumnWidth(3,200);
    ui->treeWidget->setColumnWidth(4,30);
    ui->treeWidget->setColumnWidth(5,50);
    ui->treeWidget->setColumnWidth(6,30);
    ui->treeWidget->setColumnWidth(7,300);
    ui->treeWidget->setColumnWidth(8,50);
    ui->treeWidget->setColumnWidth(9,200);
    /*ui->treeWidget->setStyleSheet(
                "QHeaderView::section{font:14pt '微软雅黑';}"
                "QTreeView::item:hover{background-color:rgb(85, 170, 255)}"
                "QTreeView::item:selected{background-color:rgb(85, 170, 255)}"
                );
    */

    /***************/
    this->setStyleSheet("QMainWidget{background-color:rgba(0,0,0);}"
                        "QPushButton{background-color: rgba(255, 255, 255, 0);border:1px solid gray;border-radius:10px;}"
                        "QPushButton:hover{background-color: rgba(85, 170, 255, 0);border:1px solid black;border-radius:10px;background:rgb(85, 170, 255);}"
                        "QPushButton:press{background-color: rgba(85, 170, 255, 0);border:1px solid black;border-radius:10px;background:rgb(85, 170, 255);}"
                        // "QComboBox{background-color: rgba(255, 255, 255, 0);border:2px solid black;border-radius:2px;}"
                        );
    one = new QAction("Left",ui->treeWidget);
    two = new QAction("Right",ui->treeWidget);
    List << one << two;
    menu1 = new QMenu(ui->treeWidget);
    connect(ui->treeWidget, SIGNAL(itemPressed(QTreeWidgetItem*,int)), this, SLOT(itemPressedSlot(QTreeWidgetItem*,int)));
    connect(one, SIGNAL(triggered(bool)), this, SLOT(function()));
    connect(two, SIGNAL(triggered(bool)), this, SLOT(function2()));
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_Read1_clicked()
{

    QString directory=QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this,tr("Save path"),QDir::homePath()));

    if (!directory.isEmpty()) {
        if(ui->comboBox->findText(directory)==-1)
            ui->comboBox->addItem(directory);
        ui->comboBox->setCurrentIndex(ui->comboBox->findText(directory));
    }
    path1 = ui->comboBox->currentText().toStdString().data();
}

void MainWindow::on_Read2_clicked()
{

    QString directory=QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this,tr("Save path"),QDir::homePath()));
    if (!directory.isEmpty()) {
        if(ui->comboBox_2->findText(directory)==-1)
            ui->comboBox_2->addItem(directory);
        ui->comboBox_2->setCurrentIndex(ui->comboBox_2->findText(directory));
    }
    path2 = ui->comboBox_2->currentText().toStdString().data();
}

void MainWindow::on_pushButton_2_clicked()
{
    if (path1==""&&path2!="") {
        QMessageBox::information(NULL, "Tip", "Please input left path!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        qDebug()<<"Please input left path!";
        return ;
    } else if (path2==""&&path1!="") {
        QMessageBox::information(NULL, "Tip", "Please input right path!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        qDebug()<<"Please input right path!";
        return ;
    } else if (path1==""&&path2=="") {
        QMessageBox::information(NULL, "Tip", "Please input left and right path!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        qDebug()<<"Please input left and right path!";
        return ;
    }

    backupone backone;
    backuptwo backtwo;
    GetFile get;
    if (flag == 0) {
        int click2 = QMessageBox::information(NULL, "Tip", "Are you sure to start to sync?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if (click2 == QMessageBox::No) {
            return ;
        }
        int sum = get.getnum1(path2, path1);
        if (sum) {
            if (path2.contains(path1) || path1.contains(path2)){
                QMessageBox message(QMessageBox::Warning,"Warning!!!","<font size='26' color='red'>Your backup directory contains the source directory, please choose carefully!</font>", QMessageBox::Yes | QMessageBox::No, NULL);
                int click = message.exec();
                if (click == QMessageBox::No) {
                    return ;
                }
                if (click == QMessageBox::Yes) {
                    QMessageBox message(QMessageBox::Warning,"Warning!!!","<font size='26' color='red'>Are you sure continue?</font>", QMessageBox::Yes | QMessageBox::No, NULL);
                    int click = message.exec();
                    if (click == QMessageBox::No) {
                        return ;
                    }
                }
            }

            QProgressDialog* dialog = new QProgressDialog(this);
            backone.backup_one(path2, path1,dialog);
        }
    } else if (flag == 1) {
        int click2 = QMessageBox::information(NULL, "Tip", "Are you sure to start to sync?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if (click2 == QMessageBox::No) {
            return ;
        }
        int sum = get.getnum1(path1, path2);
        if (sum) {
            if (path2.contains(path1) || path1.contains(path2)){
                QMessageBox message(QMessageBox::Warning,"Warning!!!","<font size='26' color='red'>Your backup directory contains the source directory, please choose carefully!</font>", QMessageBox::Yes | QMessageBox::No, NULL);
                int click = message.exec();
                if (click == QMessageBox::No) {
                    return ;
                }
                if (click == QMessageBox::Yes) {
                    QMessageBox message(QMessageBox::Warning,"Warning!!!","<font size='26' color='red'>Are you sure continue?</font>", QMessageBox::Yes | QMessageBox::No, NULL);
                    int click = message.exec();
                    if (click == QMessageBox::No) {
                        return ;
                    }
                }
            }
            QProgressDialog* dialog = new QProgressDialog(this);
            backone.backup_one(path1, path2, dialog);
        }
    } else if (flag == 2) {
        int click2 = QMessageBox::information(NULL, "Tip", "Are you sure to start to sync?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if (click2 == QMessageBox::No) {
            return ;
        }
        int sum = get.getnum2(path1, path2);
        if (sum) {
            QProgressDialog* dialog = new QProgressDialog(this);
            backtwo.backup_two(path1, path2, dialog);
        }
    }
}

void MainWindow::on_comboBox_3_currentIndexChanged(const QString &arg1)
{
    if (arg1.contains("Left")) {
        flag = 0;
    }
    if (arg1.contains("Right")) {
        flag = 1;
    }
    if (arg1.contains("Two")) {
        flag = 2;
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->treeWidget->clear();
    analyse ana;
    ana.analyse_one(path1, path2, ui, flag);
}

void MainWindow::on_pushButton_clicked()
{
    ui->treeWidget->clear();
    ui->comboBox->clear();
    ui->comboBox_2->clear();
    path1.clear();
    path2.clear();
    ui->comboBox_3->setCurrentIndex(0);
}

void MainWindow::on_pushButton_4_clicked()
{
    Dialog dia;
    dia.showlog();
}

void MainWindow::function()
{
    int click2 = QMessageBox::information(NULL, "Tip", "You will replace left by right!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if (click2 == QMessageBox::No) {
        return ;
    }
    QFile file("../BackupAndRestore/log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    QTreeWidgetItem* curItem=ui->treeWidget->currentItem();
    curItem->setIcon(5, QIcon(":/Icon/ok.png"));
    curItem->setIcon(6, QIcon(":/Icon/left-plus.png"));
    curItem->setIcon(4, QIcon(":/Icon/right-del.png"));
    QString name1 = path1 + curItem->text(1);
    QString name2 = path2 + curItem->text(7);
    QFile(name1).remove();
    QFile::copy(name2, name1);
    QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
    QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
    file.write((time + "  delete file:  " + name1 + '\n').toUtf8());
    file.write((time + "  add file:  " + name1 + '\n').toUtf8());
    curItem->setTextColor(0, Qt::black);
    curItem->setTextColor(1, Qt::black);
    curItem->setTextColor(2, Qt::black);
    curItem->setTextColor(3, Qt::black);
    curItem->setTextColor(7, Qt::black);
    curItem->setTextColor(8, Qt::black);
    curItem->setTextColor(9, Qt::black);
    file.close();
}

void MainWindow::function2()
{
    int click2 = QMessageBox::information(NULL, "Tip", "You will replace right by left!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if (click2 == QMessageBox::No) {
        return ;
    }
    QFile file("../BackupAndRestore/log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    QTreeWidgetItem* curItem=ui->treeWidget->currentItem();
    curItem->setIcon(5, QIcon(":/Icon/ok.png"));
    curItem->setIcon(4, QIcon(":/Icon/right-plus.png"));
    curItem->setIcon(6, QIcon(":/Icon/left-del.png"));
    QString name1 = path1 + curItem->text(1);
    QString name2 = path2 + curItem->text(7);
    QFile(name2).remove();
    QFile::copy(name1, name2);
    QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
    QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
    file.write((time + "  delete file:  " + name2 + '\n').toUtf8());
    file.write((time + "  add file:  " + name2 + '\n').toUtf8());
    curItem->setTextColor(0, Qt::black);
    curItem->setTextColor(1, Qt::black);
    curItem->setTextColor(2, Qt::black);
    curItem->setTextColor(3, Qt::black);
    curItem->setTextColor(7, Qt::black);
    curItem->setTextColor(8, Qt::black);
    curItem->setTextColor(9, Qt::black);
    file.close();
}
void MainWindow::itemPressedSlot(QTreeWidgetItem * pressedItem, int column)
{
    //QTreeWidgetItem* curItem=ui->treeWidget->currentItem();
    if (qApp->mouseButtons() == Qt::RightButton) {
        if (!pressedItem->icon(5).isNull() && column == 5) {
            menu1->addActions(List);
            menu1->exec(QCursor::pos());
        }
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this,"quit","Are you sure to quit?");
    if(ret == QMessageBox::Yes)
    {
        //关闭窗口
        event->accept();
    }else {
        //不关闭
        event->ignore();
    }
}
